package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaLearnPeriodAddDTO;
import com.cheer.dto.OaLearnPeriodUpdateDTO;
import com.cheer.service.OaLeamPeriodsService;
import com.cheer.vo.OaLearnPeriodInfoVO;
import com.cheer.vo.OaLearnPeriodListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/learn/period")
public class OaLearnPeriodController {

    @Autowired
    private OaLeamPeriodsService oaLeamPeriodsService;

    @ApiOperation("添加学习")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaLearnPeriodAddDTO learnPeriodAddDTO) {
        oaLeamPeriodsService.add(learnPeriodAddDTO);
        return Result.success();
    }

    @ApiOperation("查询学习")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaLearnPeriodListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(oaLeamPeriodsService.list(pageNum, pageSize));
    }

    @ApiOperation("删除学习")
    @GetMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestParam Integer id) {
        oaLeamPeriodsService.removeById(id);
        return Result.success();
    }

    @ApiOperation("查询学习详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaLearnPeriodInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaLeamPeriodsService.info(id));
    }

    @ApiOperation("修改学习")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaLearnPeriodUpdateDTO learnPeriodUpdateDTO) {
        oaLeamPeriodsService.update(learnPeriodUpdateDTO);
        return Result.success();
    }
}
